﻿using System;
using com.yoozoo.gta.Extension.UGUI.Editor;
using UnityEngine;

namespace com.yoozoo.gta.Extension.UGUI.ScreenOrientationEx
{
    
    [Serializable]
    public class RectTransformAdaptConfig : AdaptBaseConfig
    {
        public bool useIsActive = false;
        [ReadOnlyAdapter]public bool isActive;
        [ReadOnlyAdapter]public Vector2 anchorMin;
        [ReadOnlyAdapter]public Vector2 anchorMax;
        [ReadOnlyAdapter]public Vector2 sizeDelta;
        [ReadOnlyAdapter]public Vector3 localPosition;
        [ReadOnlyAdapter]public Vector2 anchoredPosition;
        [ReadOnlyAdapter]public Vector3 localEulerAngles;
        [ReadOnlyAdapter]public Vector3 localScale;
        [ReadOnlyAdapter]public Vector2 pivot;
        [ReadOnlyAdapter]public Transform parent = null;
        [ReadOnlyAdapter]public int childIndex = DEFAULT_VALUE;
        public bool useParent = false; //是否修改父节点
        public bool ignore = false;//是否忽略适配
        
        //重写Equals方法
        public override bool Equals(object obj)
        {
            if (obj == null)
            {
                return false;
            }

            RectTransformAdaptConfig other = (RectTransformAdaptConfig)obj;
            // Compare useIsActive properties
            if (useIsActive != other.useIsActive)
            {
                return false;
            }
            // Compare isActive properties
            if (isActive != other.isActive)
            {
                return false;
            }

            // Compare anchorMin properties
            if (anchorMin != other.anchorMin)
            {
                return false;
            }
            
            // Compare anchorMax properties
            if (anchorMax != other.anchorMax)
            {
                return false;
            }
            
            // Compare sizeDelta properties
            if (sizeDelta != other.sizeDelta)
            {
                return false;
            }
            
            // Compare localPosition properties
            if (localPosition != other.localPosition)
            {
                return false;
            }
            
            // Compare anchoredPosition properties
            if (anchoredPosition != other.anchoredPosition)
            {
                return false;
            }
            
            // Compare localEulerAngles properties
            if (localEulerAngles != other.localEulerAngles)
            {
                return false;
            }
            
            // Compare localScale properties
            if (localScale != other.localScale)
            {
                return false;
            }
            
            // Compare pivot properties
            if (pivot != other.pivot)
            {
                return false;
            }
            if (parent != other.parent)
            {
                return false;
            }
            
            if (childIndex != other.childIndex)
            {
                return false;
            }
            
            if (useParent != other.useParent)
            {
                return false;
            }
            
            if(ignore != other.ignore)
            {
                return false;
            }

            return true;
        }
        
        //重写GetHashCode方法
        public override int GetHashCode()
        {
            int hash = 17;
            hash = hash * 23 + useIsActive.GetHashCode();
            hash = hash * 23 + isActive.GetHashCode();
            hash = hash * 23 + anchorMin.GetHashCode();
            hash = hash * 23 + anchorMax.GetHashCode();
            hash = hash * 23 + sizeDelta.GetHashCode();
            hash = hash * 23 + localPosition.GetHashCode();
            hash = hash * 23 + anchoredPosition.GetHashCode();
            hash = hash * 23 + localEulerAngles.GetHashCode();
            hash = hash * 23 + localScale.GetHashCode();
            hash = hash * 23 + pivot.GetHashCode();
            hash = hash * 23 + parent.GetHashCode();
            hash = hash * 23 + childIndex.GetHashCode();
            hash = hash * 23 + useParent.GetHashCode();
            hash = hash * 23 + ignore.GetHashCode();
            
            return hash;
        }
        
        
    }
    [DisallowMultipleComponent]
    public class RectTransformAdapter: AdaptBase<RectTransformAdaptConfig,RectTransform>
    {
        
        public override int Order => (int)AdapterExecuteOrder.RectTransform; 
        protected override void ApplyConfig(RectTransformAdaptConfig config) {
            base.ApplyConfig(config);
            if (config.ignore) return;
            
            if (config.useIsActive) {
                gameObject.SetActive(config.isActive);
            }

            if (config.useParent)
            {
                if (config.parent != null && !UnityEngine.Object.Equals(config.parent, null))
                {
                    mComponent.SetParent(config.parent);
                    if (config.childIndex == DEFAULT_VALUE)
                    {
                        mComponent.SetAsLastSibling();
                    }
                    else
                    {
                        mComponent.SetSiblingIndex(config.childIndex);
                    }
                }
            }  
            
            
            mComponent.anchorMin = config.anchorMin;
            mComponent.anchorMax = config.anchorMax;
            mComponent.sizeDelta = config.sizeDelta;
            mComponent.anchoredPosition = config.anchoredPosition;
          
            mComponent.localEulerAngles = config.localEulerAngles;
            mComponent.localScale = config.localScale;
            mComponent.pivot = config.pivot;
            
            
        }

        protected override void CopyProperty(RectTransformAdaptConfig config) {
            base.CopyProperty(config);
            #if UNITY_EDITOR
            if (mComponent.parent == null || mComponent.parent.name == "Canvas (Environment)")
            {
                config.parent = null;
                config.useParent = false;
                config.childIndex = DEFAULT_VALUE;
                //config.ignore = true;
            }
            else
            {
                config.parent = mComponent.parent;
                config.childIndex = mComponent.GetSiblingIndex();
            }
                
            #endif
            config.isActive = gameObject.activeSelf;
            config.anchorMin = mComponent.anchorMin;
            config.anchorMax = mComponent.anchorMax;
            config.sizeDelta = mComponent.sizeDelta;
            
            
            config.anchoredPosition = mComponent.anchoredPosition;
            config.localEulerAngles = mComponent.localEulerAngles;
            config.localScale = mComponent.localScale;
            config.pivot = mComponent.pivot;
            
           
        }

#if UNITY_EDITOR
        public bool IsUseActiveConfig()
        {
            if(LandscapeConfig.useIsActive || PortraitConfig.useIsActive)
            {
                return true;
            }

            return false;
        }
#endif
    }
}